/**
 * 安全奖i订单 编辑页
 *
 * 作者 zhangwei
 * 日期 2024-09-02 11:28:06
 */
layui.use(['jquery', 'febs', 'form', 'validate', 'table', 'commonJS'], function () {
    var $ = layui.$,
        febs = layui.febs,
        layer = layui.layer,
        form = layui.form,
        table = layui.table,
        commonJS = layui.commonJS,
        userTableIns,
        goodsTableIns,
        $view = $('#incentiveGoodsOrder-detail'),

        validate = layui.validate;
    var tenantId = POPUP_DATA.tenantId;

    initFormValue();

    function initFormValue() {
        if (incentiveGoodsOrder) {
            if(incentiveGoodsOrder.goodsOrderType){
                let type = "";
                if(incentiveGoodsOrder.goodsOrderType === "1") {
                    type = "安全奖励"
                }else if(incentiveGoodsOrder.goodsOrderType === "2") {
                    type = "线上申请"
                }else if(incentiveGoodsOrder.goodsOrderType === "3") {
                    type = "线下兑换"
                }
                $view.find("#goodsOrderType").text(type)
            }
            if(incentiveGoodsOrder.orderDate) {
                $view.find("#orderDate").text(incentiveGoodsOrder.orderDate.substring(0,10));
            }
            if(incentiveGoodsOrder.exchangeDate) {
                $view.find("#exchangeDate").text(incentiveGoodsOrder.exchangeDate.substring(0,10));
            }
            if(incentiveGoodsOrder.status) {
                let status = ""
                if(incentiveGoodsOrder.status === "1") {
                    status = "待提交"
                }else if(incentiveGoodsOrder.status === "2") {
                    status = "待领取"
                }else if(incentiveGoodsOrder.status === "3") {
                    status = "已领取"
                }else if(incentiveGoodsOrder.status === "4") {
                    status = "已取消"
                }
                $view.find("#status").text(status);
            }
            if(!commonJS.isEmpty(incentiveGoodsOrder.userList)){
                initUserTable(incentiveGoodsOrder.userList);
            }else {
                initUserTable([]);
            }
            if(!commonJS.isEmpty(incentiveGoodsOrder.goodsList)){
                initGoodsTable(incentiveGoodsOrder.goodsList);
            }else {
                initGoodsTable([]);
            }
        }
    }

    function initUserTable(data) {
        userTableIns = febs.table.init({
            elem: $view.find('table[id="GoodsOrderUserTable"]'),
            id: 'GoodsOrderUserTable',
            page: false,
            limit: Number.MAX_VALUE,
            data: data,
            skin: '',
            cols: [[
                {type: 'numbers', title: '序号'},
                {field: 'tenantName', title: '所属公司', minWidth: 120},
                {field: 'userNo', title: '用户编号', width: 150, align: "center"},
                {field: 'realName', title: '用户姓名', width: 150, align: "center"},
                {field: 'deptName', title: '部门名称', align: "center", width: 150},
            ]],
            defaultToolbar: []
        });
        form.render()
    }

    function initGoodsTable(data) {
        goodsTableIns = febs.table.init({
            elem: $view.find('table[id="GoodsOrderTable"]'),
            id: 'GoodsOrderTable',
            page: false,
            limit: Number.MAX_VALUE,
            data: data,
            skin: '',
            cols: [[
                {type: 'numbers', title: '序号'},
                {field: 'goodsName', title: '奖品名称', minWidth: 120},
                {field: 'inventory', title: '在库数量', width: 150, align: "center"},
                {field: 'exchangeScore', title: '兑换积分', width: 150, align: "center"},
                {field: 'goodsNumber', title: '奖品数量', edit: 'text', align: "center", width: 150},
            ]],
            defaultToolbar: []
        });
        form.render()
    }

    // function reloadTab(data) {
    //     tableIns.reload({data: data});
    // }

    function getTableHeight() {
        return commonJS.calcTableHeight();
    }
});